﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.Common;
using WX.WMS.DataModel;

namespace WX.WMS.BLL
{
    public class BaseBLL
    {
        public BaseBLL()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        private string _WhereCondition = " 1==1 ";
        /// <summary>
        /// 
        /// </summary>
        private string _OrderByCondition = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private string EqualTemplate = " {0} == {1} ";
        /// <summary>
        /// 
        /// </summary>
        private string NotEqualTemplate = " {0} != {1} ";
        /// <summary>
        /// 
        /// </summary>
        private string StringEqualTemplate = " {0}.Equals(\"{1}\") ";
        /// <summary>
        /// 
        /// </summary>
        private string StringNotEqualTemplate = " !{0}.Equals(\"{1}\") ";
        /// <summary>
        /// 
        /// </summary>
        private string LikeTemplate = " {0}.Contains(\"{1}\") ";
        /// <summary>
        /// 
        /// </summary>
        private string BetweenTemplate = " {0} >= {1} And {2} <= {3} ";
        
        /// <summary>
        /// 
        /// </summary>
        public string WhereCondition
        {
            get { return _WhereCondition; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string OrderCondition
        {
            get {
                if(_OrderByCondition==string.Empty) 
                    return _OrderByCondition+" ID "; 
                else
                    return _OrderByCondition + " ,ID "; 
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="FieldIsString"></param>
        public void AddNotEqualCondition(string field, string value, bool FieldIsString)
        {
            if (FieldIsString)
            {
                _WhereCondition += " And " + string.Format(StringNotEqualTemplate, field, value);
            }
            else
            {
                AddNotEqualCondition(field, value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddNotEqualCondition(string field, string value)
        {
            _WhereCondition += " And " + string.Format(NotEqualTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddNotEqualCondition(string field, decimal value)
        {
            _WhereCondition += " And " + string.Format(NotEqualTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="FieldIsString"></param>
        public void AddEqualCondition(string field, string value, bool FieldIsString)
        {
            if (FieldIsString)
            {
                _WhereCondition += " And " + string.Format(StringEqualTemplate, field, value);
            }
            else
            {
                AddEqualCondition(field, value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddEqualCondition(string field, string value)
        {
            _WhereCondition += " And " + string.Format(EqualTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddEqualCondition(string field, decimal value)
        {
            _WhereCondition += " And " + string.Format(EqualTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void AddLikeCondition(string field, string value)
        {
            _WhereCondition += " And " + string.Format(LikeTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        public void OrLikeCondition(string field, string value)
        {
            _WhereCondition += " Or " + string.Format(LikeTemplate, field, value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="beginValue"></param>
        /// <param name="endValue"></param>
        /// <param name="FieldIsTime"></param>
        public void AddBetweenCondition(string field, string beginValue, string endValue, bool FieldIsTime)
        {
            if (FieldIsTime)
            {
                if (beginValue == string.Empty && endValue != string.Empty)
                        _WhereCondition += " And " + string.Format("{0}<={1}", field, "DateTime.Parse(\"" + endValue + "\")");
                if (beginValue != string.Empty && endValue == string.Empty)
                        _WhereCondition += "And " + string.Format("{0}>={1}", field, "DateTime.Parse(\"" + beginValue + "\")");
                if(beginValue!=string.Empty && endValue!=string.Empty)
                        _WhereCondition += " And " + string.Format(BetweenTemplate, field, "DateTime.Parse(\"" + beginValue + "\")", field, "DateTime.Parse(\"" + endValue + "\")");
            }
            else
            {
                AddBetweenCondition(field, beginValue, endValue);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="beginValue"></param>
        /// <param name="endValue"></param>
        public void AddBetweenCondition(string field, string beginValue, string endValue)
        {
            _WhereCondition += " And " + string.Format(BetweenTemplate, field, beginValue, field, endValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderBy"></param>
        public void AddOrderByCondition(string orderBy)
        {
            if (_OrderByCondition != string.Empty)
            {
                _OrderByCondition += " , ";
            }
            _OrderByCondition += orderBy;
        }

        

        /// <summary>
        /// 
        /// </summary>
        public void ClearWhereCondition()
        {
            this._WhereCondition = " 1==1 ";
        }

        public void ClearOrderByCondition()
        {
            this._OrderByCondition = string.Empty;
        }

        public int GetNextId(string sequenceName)
        {
            return new BaseDAO().GetNextId(sequenceName);
        }

        public DateTime GetDataBaseTime()
        {
            return new BankDAO().GetDatabaseTime();
        }
    }
}
